//
//  HSPLatestViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPLatestViewController.h"
#import "BaseHeader.h"
#import "AFNBlock.h"
@interface HSPLatestViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@end

@implementation HSPLatestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [UIColor whiteColor]];
    self.firstArray = [[NSMutableArray alloc]init];
    self.secondArray = [[NSMutableArray alloc]init];
    [NSTimer scheduledTimerWithTimeInterval:3000000000. target:self selector:@selector(changeCountFirst:) userInfo:nil repeats:YES];
    self.index = 0;
  
    [self tableViewFirst];
    [self scrollTitle];
    [self tablelunbo];
    [self AlbumaHeader];
    [self AlbumaFooter];
   
}
- (void)tableViewFirst
{
    _tableLatest = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 147) style:UITableViewStylePlain];
    _tableLatest.delegate =self;
    _tableLatest.dataSource = self;
    [self.view addSubview:_tableLatest];
}
- (void)scrollTitle
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ADJUSTHEIGHT * 180)];
    self.tableLatest.tableHeaderView = headView;
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ADJUSTHEIGHT * 180)];
    _scrollView.contentSize = CGSizeMake(8 * SCREEN_WIDTH, 0);
    [_scrollView setPagingEnabled:YES];
    [_scrollView showsHorizontalScrollIndicator];
    _scrollView.showsHorizontalScrollIndicator = NO;
    [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    [headView addSubview:_scrollView];
  
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _firstArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    HSPLatestModel *modelLatest = self.firstArray[indexPath.row];
    self.tableLatest.scrollsToTop = YES;
    [cell getImage:modelLatest];
    return cell;
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPLatestViewController *vc = self;
    [_tableLatest addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableLatest.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPLatestViewController *vcc = self;
    [_tableLatest addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/1?pageNo=%ld&pageSize=20&v=4.0.0",_page] block:^(id back) {
            if (_isUp == YES) {
                [self.firstArray removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"data"]) {
                HSPLatestModel *model = [HSPLatestModel modelWithDictionary:dic];
                [self.firstArray addObject:model];
            }
            [self.tableLatest reloadData];
            [self.tableLatest.header endRefreshing];
            [self.tableLatest.footer endRefreshing];
        }];
}
}
- (void)tablelunbo
{
                if ([Reachability isConnectedInternet] == YES) {
                  [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/1?pageNo=1&pageSize=20&v=4.0.0"] block:^(id back) {
                //轮播图
                for (NSDictionary *dic in [back objectForKey:@"focus"]) {
                    HSPLatestModel *imageModel = [HSPLatestModel modelWithDictionary:dic];
                    [self.secondArray addObject:imageModel];
                }
                NSString *oldLast = [self.secondArray lastObject];
                NSString *oldFirst = [self.secondArray firstObject];
                NSString *newFirst = oldLast;
                NSString *newLast = oldFirst;
                [self.secondArray insertObject:newFirst atIndex:0];
                [self.secondArray addObject:newLast];
                
                for (int i = 0; i < self.secondArray.count; i++) {
                    HSPLatestModel *imageModel = [self.secondArray objectAtIndex:i];
                    self.imagePush = [[UIImageView alloc]initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, ADJUSTHEIGHT * 180)];
                    [self.imagePush sd_setImageWithURL:[NSURL URLWithString:imageModel.image]placeholderImage:[UIImage imageNamed:@"1.jpg"]];
                    self.imagePush.tag = i;
                    self.imagePush.userInteractionEnabled = YES;
                    [_scrollView addSubview:_imagePush];
                    
                    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                    button.frame =CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, ADJUSTHEIGHT * 180);
                    button.backgroundColor = [UIColor clearColor];
                    [_scrollView addSubview:button];
                    button.tag = i;
                    [button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
                    
                    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i *SCREEN_WIDTH + 10, ADJUSTHEIGHT * 160, SCREEN_WIDTH, 20 * ADJUSTHEIGHT)];
                    label.textColor = [UIColor whiteColor];
                    label.font =[UIFont systemFontOfSize:19.];
                    label.text = imageModel.title;
                    [_scrollView addSubview:label];
                    [_tableLatest reloadData];
            }
       
        }];
       
    }

}
//手指离开滑动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.timerChange = [NSTimer scheduledTimerWithTimeInterval:300000000. target:self selector:@selector(changeCountFirst:) userInfo:nil repeats:YES];
}
//定时滑动
- (void)changeCountFirst:(NSTimer *)timer
{

    [UIView animateWithDuration:1 animations:^{
        [_scrollView setContentOffset:CGPointMake((self.index + 1) * SCREEN_WIDTH, 0)];
        
    }];
        self.index ++;
        if (_scrollView.contentOffset.x == 7 * SCREEN_WIDTH) {
            [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];
            self.index = 0;
        }
    }


//手指接触
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timerChange invalidate];
    self.timerChange = nil;
}
//减速滑动调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_scrollView == scrollView) {
        if (_scrollView.contentOffset.x >= SCREEN_WIDTH * 7) {
            [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
            
        }
        if (_scrollView.contentOffset.x <= 0) {
            [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH * 6, 0) animated:NO];
        }
    }
    self.index = _scrollView.contentOffset.x / SCREEN_WIDTH - 1;
}
- (void)Click:(UIButton *)sender
{
    NSLog(@"sssssss");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
